<template>
  <div class="Project">
    <Management>
      <Top
        slot="top"
        @showModel="showModel"
        @showAll="showAll"
        @search="onSearch"
        @MakeExcel="makeExcel"
        :excel="true"
        :add="false"
      ></Top>
      <Table
        slot="body"
        :title="columns"
        :tableData="tableData"
        :operationWidth="100"
        @other="check"
        :show-edit="false"
        :show-del="false"
        :current="current"
        other-txt="审核"
        @changePage="changePage"
        :total="total"
      ></Table>
    </Management>
<!--    <Dialog :show="isShow" :row="this.row" @confirm="confirm" @cancel="cancel">-->
<!--      <div slot="form">sd</div>-->
<!--    </Dialog>-->
    <Card
      :list="this.row"
      :show="showing"
      @confirm="CardConfirm"
      @cancel="CardCancel"
      @noPass="noPass"
    >
      <div name="card">ss</div>
    </Card>
  </div>
</template>

<script>
import Table from "@/components/common/table/Table";
import Management from "@/components/common/management/Management";
import Top from "../component/Top";
import Dialog from "../component/Dialog";
import Card from "../component/Card";

import {
  getDataByPageAndSize,
  Check,
  LikeSearch,
    ToExcel,
} from "@/network/Base/b-practice";
import {downloadOfA} from "@/common";
export default {
  components: {
    Table,
    Top,
    Management,
    Dialog,
    Card,
  },
  data() {
    const columns = [
      {
        title: "id",
        dataIndex: "id",
      },
      {
        title: "学号",
        dataIndex: "studentId",
      },
      {
        title: "学生姓名",
        dataIndex: "name",
      },
      {
        title: "实习企业",
        dataIndex: "company",
      },
      {
        title: "开始时间",
        dataIndex: "startTime",
      },
      {
        title: "结束时间",
        dataIndex: "endTime",
      },
      {
        title: "是否对口",
        dataIndex: "suitable",
      },
      {
        title: "是否完成实习",
        dataIndex: "finish",
      },
      {
        title: "是否在实习基地",
        dataIndex: "isBase",
      },
      {
        title: "附件文件",
        dataIndex: "file",
        width: 150,
      },
      {
        title: "审核结果",
        dataIndex: "check",
      },
    ];
    return {
      searchName : "",
      columns,
      isShow: false,
      showing: false,
      tableData: [],
      total: 0,
      row: {},
      current: 1,
    };
  },
  created() {
    this.init();
  },
  methods: {
   async makeExcel() {
      let message = 1;
      let description = "导出成功";
      console.log(this.searchName.length);
      if( this.searchName.length == 0 ){
        message = 0;
        description = "请先搜索是否有合适的年份数据,再导出"
      }
      else if( this.searchName.length != 4 || !/^[1-9]+[0-9]*]*$/.test(this.searchName) ){
        message = 0;
        description = "请输入正确的年份"
      }
      this.$notification.open({
        key: message ? "success" : "updatable",
        message: message ? "导出成功" : "导出错误",
        description:description
      });
      if (message == 1) {
        const res = await ToExcel(this.searchName);
        downloadOfA( new Blob([res], { type: 'application/vnd.ms-excel' })
        , `${this.searchName}学生实习信息.xlsx`,`${this.searchName}学生实习信息.xlsx`);
      } else {
        console.log(123);
      }
    },
    /**
     * @description: 初始化页面
     * @param {Number} page
     * @param {Number} size
     * @author: tao
     */
    async init(page = 1, size = 10) {
      const result = await getDataByPageAndSize(page, size);
      console.log(result);
      if (result.msg) {
        this.tableData = result.data.slice();
        this.total = result.count;
        this.current = page;
      }
    },

    async del(e) {
      console.log(e.id);
      const result = await deleteById(e.id);
      console.log(result);
      if (result.msg) {
        this.init();
      }
    },

    /**
     * @description: 点击弹窗 确定 按钮
     * @param {Object} data
     * @author: tao
     */
    async confirm(data) {
      // console.log(data);
      this.closeDialog();
      this.$notification["success"]({
        message: "已经更新啦！",
        description: "已更新所有消息",
      });
    },

    changePage(page) {
      this.init(page);
    },

    async onSearch(value) {
      this.searchName = value;
      const result = await LikeSearch(value);
      if (result.msg) {
        this.tableData = result.data.slice();
        this.total = result.count;
        //this.current = page;
      }
      // this.tableData = result.data.map((item) => {
      //   item.type = paperType[+item.type];
      //   item.check = check[+item.check];
      //   return item;
      // });

      if (result.msg) {
        this.data = result.data;
        this.total = result.count;
      }
    },

    async checked(e) {
      console.log(e);
      this.closeDialog();
    },

    showAll() {
      this.$notification["success"]({
        message: "显示成功！",
        description: "已显示所有消息",
      });
      this.init();
    },

    showModel() {
      this.isShow = true;
      this.row = { time: null };
    },

    edit(e) {
      this.isShow = true;
      this.row = { ...e, edit: true };
      let member = this.row.member;
    },
    handleCheck(e) {
      this.checking = true;
      this.row = { ...e, checking: true };
    },
    cancel() {
      this.closeDialog();
    },
    async noPass(e){
      let axiosPromise = await Check( e.id,2 );
      console.log(axiosPromise);
      if( axiosPromise.msg ){
        this.$notification.open({
          key: "success",
          message: "操作成功" ,
          description:"对实习信息审核成功"
        });
      }
      this.closeDialog();
      await this.init();

    },
    check(e) {
      this.showing = true;
      this.row = { ...e, edit: true };
      console.log("审核");
    },
    closeDialog() {
      this.isShow = false;
      this.checking = false;
      this.showing = false;
      this.row = { time: null };
    },
    async CardConfirm(e) {
      let axiosPromise = await Check( e.id,1 );
      console.log(axiosPromise);
      if( axiosPromise.msg ){
        this.$notification.open({
          key: "success",
          message: "操作成功" ,
          description:"对实习信息审核成功"
        });
      }
      this.closeDialog();
      await this.init();
    },
    CardCancel() {
      this.closeDialog();
    },
  },
};
</script>


<style lang="less" scoped>
</style>